#!/usr/bin/python
# -*- coding: utf-8 -*-
# Diamanda Application Set
# myghtyboard forum

from datetime import datetime

from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.utils.translation import ugettext as _
from django.views.generic.list_detail import object_list
from django.db.models import Q
from django.core.mail import mail_admins
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator,InvalidPage,EmptyPage

from diamandas.myghtyboard.models import *
from diamandas.myghtyboard.context import forum as forumContext
from diamandas.utils import *
from diamandas.myghtyboard.modelwrappers import AddTopicForm, AddPostForm




def category_list(request):
	"""
	show all categories and their topics
	"""
	categories = Category.objects.all().order_by('order')
	for c in categories:
		#{% if forum.mods %}<u>{% trans "Moderators" %}</u>:
		#        {% for i in forum.mods %}
		#               {{ i.username }}{% if not forloop.last %},{% endif %} 
		#        {% endfor %}
		#{% endif %}
		#forum = c.forum_set.all().order_by('order')
		#forums = []
		#for f in forum:
		#	f.mods = f.moderators.all()
		#	forums.append(f)
		c.forums = c.forum_set.all().order_by('order')
		for f in c.forums:
		  f.isNew = f.hasNewPost(request.user)
	hot_posts = Topic.objects.extra(select={'modification_date':'select date from myghtyboard_post where myghtyboard_post.topic_id = myghtyboard_topic.id order by date desc limit 1 offset 0'}).order_by('-modification_date')[:3]
	for t in hot_posts:
		t.first_unread = t.get_first_unread(request.user)
	hot_topics = Topic.objects.all().order_by('-id')[:3]
	return render_to_response(
		'myghtyboard/category_list.html',
		{'categories': categories,'hot_topics':hot_topics,'hot_posts':hot_posts},
		context_instance=RequestContext(request,processors=[forumContext]))


def topic_list(request, forum_id, pagination_id=1):
	"""
	list of topics in a forum
	
	* forum_id - id of a Forum record
	"""
	#prefixes = False
	#prefixes_list = False
	#if request.POST:
	#	prefixes = request.POST.copy()
	#	prefixes = prefixes.getlist("prefix[]")
	#	prefixes = Prefix.objects.filter(id__in=prefixes)

	try:
		#if prefixes and len(prefixes) > 0:
		#	tops = TopicPrefix.objects.all().values('topic').distinct()
		#	for i in prefixes:
		#		tops = tops.filter(prefix=i)
		#	topics_ids = []
		#	for tid in tops:
		#		topics_ids.append(tid['topic'])
		#	topics = Topic.objects.order_by('-is_global', '-is_sticky', '-modification_date').filter(Q(forum=forum_id) | Q(is_global='1'))
		#	topics = topics.filter(id__in=topics_ids)
		#else:
		#	topics = Topic.objects.order_by('-is_global', '-is_sticky', '-modification_date').filter(Q(forum=forum_id) | Q(is_global='1'))
		topics = Topic.objects.extra(select={'modification_date':'select date from myghtyboard_post where myghtyboard_post.topic_id = myghtyboard_topic.id order by date desc limit 1 offset 0'}).order_by('-is_global', '-is_sticky','-modification_date').filter(Q(forum=forum_id) | Q(is_global='1'))
		for t in topics:
		  t.hasNew = t.hasNewPost(request.user)
		  if t.hasNew:
		  	t.first_unread = t.get_first_unread(request.user)
		#count = topics.count()/10
		#cnt = range(1,max(1,count)+1)
		#p = Paginator(all_topics,10)
		#try:
		#	topics = p.page(pagination_id)
		#except InvalidPage:
		#	topics = p.page(1)
		#count = count/10
		#cnt = [1]
		#i = 1
		#while i <= count:
		#	i = i+1
		#	cnt.append(i)
		forum = Forum.objects.get(id=forum_id)
		name = forum.name
	except:
		return redirect_by_template(request, "/forum/", _('There is no such forum. Please go back to the forum list.'))
	form = AddTopicForm()
	
	#pr = False
	#if forum.use_prefixes:
	#	p = Prefix.objects.filter(forums=forum)
	#	if len(p) > 0:
	#		pr = []
	#		for i in p:
	#			pr.append(i)
	request.forum_id = forum_id
	#hot_posts = Topic.objects.filter(forum__id=forum_id.extra(select={'modification_date':'select date from myghtyboard_post where myghtyboard_post.topic_id = myghtyboard_topic.id order by date desc limit 1 offset 0'}).order_by('-modification_date')[:3]
	hot_topics = Topic.objects.filter(forum__id=forum_id).order_by('-id')[:3]
	return render_to_response('myghtyboard/topics_list.html',
	{'topics':topics,'forum': forum, 'form': form, 'current_user': str(request.user),'hot_topics':hot_topics},
	context_instance=RequestContext(request, processors=[forumContext])
	)
	#return object_list(
	#	request,
	#	topics,
	#	paginate_by = 10,
	#	allow_empty = True,
	#	page = pagination_id,
	#	context_processors = [forumContext],
	#	extra_context = {'forum': forum, 'form': form, 'current_user': str(request.user), 'cnt': cnt,'hot_topics':hot_topics},
	#	template_name = 'myghtyboard/topics_list.html')

#@login_required
#def my_topic_list(request, show_user=False):
#	"""
#	list my topics
#	
#	* show_user - if not set will show current user topics
#	"""
#	if not show_user:
#		show_user = str(request.user)
#	topics = Topic.objects.order_by('-modification_date').filter(author=show_user)[:50]
#	name = _('User Topics')
#	return render_to_response(
#		'myghtyboard/mytopics_list.html',
#		{'topics': topics, 'name': name},
#		context_instance=RequestContext(request, forumContext(request)))

#@login_required
#def last_topic_list(request):
#	"""
#	 list last active topics
#	"""
#	topics = Topic.objects.order_by('-modification_date')[:50]
#	for i in topics:
#		pmax =  i.post_set.all().count()/10
#		pmaxten =  i.post_set.all().count()%10
#		if pmaxten != 0:
#			i.pagination_max = pmax+1
#		else:
#			i.pagination_max = pmax
#	name = _('Last Active Topics')
#	return render_to_response(
#		'myghtyboard/mytopics_list.html',
#		{'topics': topics, 'name': name},
#		context_instance=RequestContext(request, forumContext(request)))

#@login_required
#def my_posttopic_list(request, show_user=False):
#	"""
#	list topics with my posts
#	
#	* show_user - if not set will show current user topics
#	"""
#	if not show_user:
#		show_user = str(request.user)
#	try:
#		topics = Post.objects.order_by('-date').filter(author=show_user).values('topic').distinct()[:50]
#		posts = []
#		for i in topics:
#			posts.append(int(i['topic']))
#		topics = Topic.objects.order_by('-modification_date').filter(id__in=posts)
#		for i in topics:
#			pmax =  i.post_set.all().count()/10
#			pmaxten =  i.post_set.all().count()%10
#			if pmaxten != 0:
#				i.pagination_max = pmax+1
#			else:
#				i.pagination_max = pmax
#		name = _('User Posts in Latest Topics')
#	except:
#		return render_to_response('myghtyboard/mytopics_list.html', {}, context_instance=RequestContext(request, forumContext(request)))
#	return render_to_response(
#		'myghtyboard/mytopics_list.html',
#		{'topics': topics, 'name': name},
#		context_instance=RequestContext(request, forumContext(request)))

def post_list(request, topic_id, pagination_id, post_id=None):
	"""
	 list post in topic with a generic pagination view
	
	* topic_id - id of a Topic entry
	"""
	try:
		topic = Topic.objects.get(id=topic_id)
	except Topic.DoesNotExist:
		return HttpResponseRedirect('/forum/')
	if  topic.is_locked:
		opened = False
	else:
		opened = True
	if request.user == topic.first_post().author_system:
		is_author = True
	else:
		is_author = False
	forum = topic.forum
	request.forum_id = forum.id
	form = AddPostForm()
	posts = topic.post_set.all().order_by('date')
	count = posts.count()/10
	cnt = range(1,count+2)
	
	start = (int(pagination_id) - 1)*10
	stop = start + 10

	  
	p = Paginator(posts,10)
	try:
		posts = p.page(pagination_id)
		for post in posts.object_list:
	  		post.new = post.isNew(request.user)
	  		post.setRead(request.user)	
	except:
		posts = p.page(1)	
	#count = count/10
	
	#cnt = [1]
	#i = 1
	
	#while i <= count:
	#	i = i+1
	#	cnt.append(i)
	hot_topics = Topic.objects.filter(forum__id=topic.forum.id).order_by('-id')[:3]
	return render_to_response(
		'myghtyboard/post_list.html',
		{
			'posts' : posts,
			'opened': opened,
			'is_author': is_author,
			'topic': topic,
			'cnt': cnt,
			'forum_id': forum.id,
			'form': form,
			'forum_name': forum,
			'hot_topics':hot_topics,
			'page': pagination_id
#			'current_user': str(request.user)
		},
		context_instance=RequestContext(request,processors=[forumContext])
	)
	#return object_list(
	#	request,
	#	posts,
	#	paginate_by = 10,
	#	page = pagination_id,
	#	context_processors = [forumContext],
	#	extra_context = {
	#		'opened': opened,
	#		'is_author': is_author,
	#		'topic': topic,
	#		'cnt': cnt,
	#		'forum_id': forum.id,
	#		'form': form,
	#		'forum_name': forum,
	#		'hot_topics':hot_topics
#	#		'current_user': str(request.user)
	#	},
	#	template_name = 'myghtyboard/post_list.html')


